<?php
class BooksLine_Action extends Typecho_Widget implements Widget_Interface_Do
{
    private $db;
    private $prefix;

    public function insertBook()
    {
        if (BooksLine_Plugin::form('in')->validate()) {
            $this->widget('Widget_Notice')->set(_t('书籍添加失败'), NULL, 'faild');
            $this->response->goBack();
        }
        $boo = $this->request->from('name', 'author', 'description', 'readtime');
        $book['book_name'] = $boo['name'];
        $book['book_author'] = $boo['author'];
        $book['book_description'] = $boo['description'];
        $book['read_time'] = $boo['readtime'];
        $this->db->query($this->db->insert($this->prefix . 'booksline')->rows($book));
        $this->widget('Widget_Notice')->set(_t('书籍添加成功'), NULL, 'success');
        $this->response->redirect(Helper::url('BooksLine%2Fmanage-booksline.php'));
    }

    public function updateBook()
    {
        if (BooksLine_Plugin::form('in')->validate()) {
            $this->widget('Widget_Notice')->set(_t('书籍编辑失败'), NULL, 'faild');
            $this->response->goBack();
        }
        $boo = $this->request->from('id', 'name', 'author', 'description', 'readtime');
        $book['book_name'] = $boo['name'];
        $book['book_author'] = $boo['author'];
        $book['book_description'] = $boo['description'];
        $book['read_time'] = $boo['readtime'];
        $this->db->query($this->db->update($this->prefix . 'booksline')->rows($book)->where('id = ?', $boo['id']));
        $this->widget('Widget_Notice')->set(_t('书籍编辑成功'), NULL, 'success');
        $this->response->redirect(Helper::url('BooksLine%2Fmanage-booksline.php'));
    }
    //不是框架，是typecho插件

    public function action()
    {
        $this->db = Typecho_Db::get();
        $this->prefix = $this->db->getPrefix();
        $this->on($this->request->is('op=up'))->updateBook();
        $this->on($this->request->is('op=in'))->insertBook();
    }
}
